// 导入数据库
const db = require("../db/index.js");

// 上传用户图片接口
exports.uploadSwiper = (req,res)=>{

	let oldName = req.files[0].filename;
	let newName = Buffer.from(req.files[0].originalname, 'latin1').toString('utf8')
	fs.renameSync('./public/upload/' + oldName, './public/upload/' + newName)
	const sql = 'update setting set set_value = ? where set_name = ?'
	db.query(sql, [
		`http://127.0.0.1:3007/upload/${newName}`,
		req.body.set_name
	], (err, result) => {
		if (err) return res.cc(err)
		res.send({
			status: 0,
			message:"上传轮播图成功"
		})
	})
}
// 获取所有轮播图
exports.getAllSwiper = (req,res) => {
	const sql = "select * from setting where set_name like 'swiper%' "
	const list = [];
	db.query(sql,(err,result)=>{
		if (err) return res.cc(err)
		result.forEach(e=>{
			list.push(e.set_value)
		})
		res.send({
			status: 0,
			result,
			list
		})
	})
}
// 获取公司名称
exports.getCompanyName = (req,res) => {
	const sql = "select * from setting where set_name = '公司名称' "
	db.query(sql,(err,result)=>{
		if (err) return res.cc(err)
		res.send({
			status: 0,
			result
		})
	})
}
// 修改公司名称
exports.changeCompanyName = (req,res) => {
	const sql = "update setting set set_value = ? where set_name = '公司名称' "
	db.query(sql,req.body.set_value,(err,result)=>{
		if (err) return res.cc(err)
		res.send({
			status: 0,
			message:'修改公司名称成功'
		})
	})
}
// 获取公司介绍信息
exports.getCompanyIntroduce = (req,res) => {
	const sql = "select set_text from setting where set_name = ? "
	db.query(sql,req.body.set_name,(err,result)=>{
		if (err) return res.cc(err)
		console.log(result,req.body.set_name)
		res.send({
			status: 0,
			result:result[0].set_text
		})
	})
}
// 修改公司介绍
exports.changeCompanyIntroduce = (req,res) => {
	const sql = "update setting set set_text = ? where set_name = ? "
	db.query(sql,[req.body.set_text,req.body.set_name],(err,result)=>{
		if (err) return res.cc(err)
		res.send({
			status: 0,
			message:'修改公司介绍成功'
		})
	})
}
// 获取公司信息
exports.getAllCompanyIntroduce = (req,res) => {
	const sql = "select * from setting where set_name like '公司%' "
	db.query(sql,(err,result)=>{
		if (err) return res.cc(err)
		res.send({
			status: 0,
			result
		})
	})
}